import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;

/**
 * 
 */

/**
 * @author weizhuowu
 *
 */
public class DChat {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args){
		//Let user choose Mode--open a group/join an existing group
		BufferedReader getUserInput = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Open a new Group: Enter 'n'\nJoin an existing group: Enter 'j'");
		try {
			if("j".equals(getUserInput.readLine())){
				joinGroup();
			}else{
				openGroup();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/*Clients join an existing group */
	public static void joinGroup(){
		BufferedReader getUserInput = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Input port number for local machine:");
		int portNum;
		String userName;
		/*Initialize UDPClient*/
		try {
			portNum = Integer.parseInt(getUserInput.readLine());
			System.out.println("Type in userName you like:");
			userName = getUserInput.readLine();		
			UDPClient client = new UDPClient(portNum, userName);
			
			System.out.print("Input IpAddress/Hostname to connect:");
			try{
				String serverIpAddress = getUserInput.readLine();
				client.setServerAddr(serverIpAddress);
			}catch(IOException ioe){
				System.out.println("Error in get Remote IpAddress!");
				System.exit(1);
			}
			System.out.print("Input remote portNumber:");
			try{
				String serverPortNum = getUserInput.readLine();
				client.setServerPortNum(Integer.parseInt(serverPortNum));
			}catch(IOException ioe){
				System.out.println("Error in get Remote Port Number!");
				System.exit(1);		
			}
			/*start reading from and writing to connected remote client*/
			ReadThread readThread = new ReadThread(client);
			WriteThread writeThread = new WriteThread(client);
			new Thread(readThread).start();
			new Thread(writeThread).start();
			/*Report to leader this client's existence, start checking leader's aliveness*/
			client.reportAlive();
			client.checkServerAlive();		
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * Leader initialize a new chat group 
	 */
	public static void openGroup(){
		BufferedReader getUserInput = new BufferedReader(new InputStreamReader(System.in));
		/*Initialize UDPServer*/
		try {
			System.out.println("Input port number for local machine:");
			int portNum = Integer.parseInt(getUserInput.readLine());
			System.out.println("Type in userName you like:");
			String userName = getUserInput.readLine();					
			UDPServer server = new UDPServer(portNum, userName);
			/*start read from and writing to connected remote client*/
			ReadThread readThread = new ReadThread(server);
			WriteThread writeThread = new WriteThread(server);
			new Thread(readThread).start();
			new Thread(writeThread).start();
			server.aliveCheck();
		} catch (NumberFormatException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}


}


